package com.suwenhao.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.suwenhao.entity.Dept;
import com.suwenhao.entity.Page;
import com.suwenhao.entity.User;
import com.suwenhao.service.MenuService;
import com.suwenhao.service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;


/**
 * @Author:suwenhao
 * @Description: 测试
 * @Date: Created in 2020/9/22 17:35
 */

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserService();

    //查询
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");

        if (username == null) {
            username = "";
        }

        Page page =new Page();
        //计算一共多少条
        Integer count = userService.count(username);
        page.setCount(count);

        Integer pageTotal;
        if (count % page.getSize() == 0) {
            pageTotal = count / page.getSize();
        } else {
            pageTotal = count / page.getSize() + 1;
        }
        page.setPageTotal(pageTotal);


        String pageStr = req.getParameter("pageCurrent");

        //当前页数
        Integer pageCurrent = 0;
        if (pageStr != null && pageStr != "") {

            pageCurrent = Integer.valueOf(pageStr);

            System.out.println(page);
        }

        if (pageCurrent <= 1) {
            pageCurrent = 1;
        }
        if (pageCurrent >= pageTotal) {
            pageCurrent=pageTotal;
        }
        page.setPageCurrent(pageCurrent);


        List<User> list = userService.userList(username, page);

        req.setAttribute("list", list);

        req.setAttribute("username", username);

        req.setAttribute("page", page);


        req.getRequestDispatcher("/html/user/list.jsp").forward(req, resp);
    }

    //添加
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        User user = new User();
//
        Map<String, String[]> map = req.getParameterMap();

        try {
            BeanUtils.populate(user, map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        userService.add(user);

        resp.sendRedirect("/user/list");


    }


    //删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");

        userService.delete(Integer.valueOf(id));

        resp.sendRedirect("/user/list");


    }

    //回写修改数据
    protected void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("toUpdate");

        String id = req.getParameter("id");

        User user = userService.getUserById(Integer.valueOf(id));
        List<Dept> list = userService.listDept();

        req.setAttribute("user", user);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/html/user/toUpdate.jsp").forward(req, resp);
//        resp.sendRedirect("/html/user/toUpdate.jsp");

    }

    //修改数据回写数据
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        User user = new User();

        Map<String, String[]> map = req.getParameterMap();

        try {
            BeanUtils.populate(user, map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        userService.update(user);

        resp.sendRedirect("/user/list");
    }


    //回写部门
    protected void listDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //回写部门
        List<Dept> list = userService.listDept();

        resp.getWriter().write(JSONObject.toJSONString(list));
    }


    //通过部门id查询该部门下的所有员工
    protected void getUserByDeptId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String deptId = req.getParameter("deptId");
        List<User> list = userService.getUserByDeptId(Integer.valueOf(deptId));

        resp.getWriter().write(JSONObject.toJSONString(list));


    }

}
